package com.ys.service.business.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ys.entity.business.YsbExportLog;
import com.ys.enume.business.YsbExportLogEnum;
import com.ys.mapper.business.YsbExportLogMapper;
import com.ys.service.business.YsbExportLogService;
import com.ys.util.shiro.ShiroUtils;

@Service("ysbExportLogService")
public class YsbExportLogServiceImpl implements YsbExportLogService {

	@Autowired
	private YsbExportLogMapper ysbExportLogMapper;

	@Override
	public ExportDto init(Long menuId) {
		
		String loginUserId = ShiroUtils.getUserId();
		
		if (new YsbExportLog().setCreateUser(loginUserId).setMenuId(menuId).setState(YsbExportLogEnum.State.PROCESSING.value).count() > 0) {
			return new ExportDto(ExportDto.Code.FAILED.value, null, "上一份报表尚未完成，不能重复操作");
		}
		
		String url = null;
		String createUser = loginUserId;
		Integer state = YsbExportLogEnum.State.PROCESSING.value;
		YsbExportLog entity = YsbExportLog.ysbExportLog(menuId, url,null, createUser, state, 1);
		
		ysbExportLogMapper.insert(entity);
		
		Long id = entity.getId();
		return new ExportDto(ExportDto.Code.SUCCESS.value, id, null);
	}

	@Override
	public void updateSuccess(Long id, String url) {
		YsbExportLog entity = new YsbExportLog().setId(id).queryFirst();
		
		entity.setUrl(url);
		entity.setState(YsbExportLogEnum.State.SUCCESS.value);
		
		ysbExportLogMapper.update(entity);
	}

	@Override
	public void updateSuccess(Long id, String url,String bid) {
		YsbExportLog entity = new YsbExportLog().setId(id).queryFirst();

		entity.setUrl(url);
		entity.setState(YsbExportLogEnum.State.SUCCESS.value);
		entity.setBid(bid);

		ysbExportLogMapper.update(entity);
	}

	@Override
	public void updateFail(Long id) {
		YsbExportLog entity = new YsbExportLog().setId(id).queryFirst();
		
		entity.setState(YsbExportLogEnum.State.FAIL.value);
		
		ysbExportLogMapper.update(entity);
	}

	@Override
	public void updateFail(Long id,String bid) {
		YsbExportLog entity = new YsbExportLog().setId(id).queryFirst();

		entity.setState(YsbExportLogEnum.State.FAIL.value);
		entity.setBid(bid);

		ysbExportLogMapper.update(entity);
	}

	@Override
	public void add(Long menuId, String url) {
		
		String createUser = ShiroUtils.getUserId();
		Integer state = YsbExportLogEnum.State.SUCCESS.value;
		YsbExportLog entity = YsbExportLog.ysbExportLog(menuId, url, null,createUser, state, 1);
		
		ysbExportLogMapper.insert(entity);
	}
}
